<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir.'/external/fpdf/fpdf.php';
require_once $basedir."/inc/gruppenkinder.php";
require_once $basedir."/inc/gruppen.php";
require_once $basedir."/inc/adress.php";
require_once $basedir."/inc/mitarbeiter.php";

class PDF extends FPDF {
    
    private $gruppe = null;
    
    public function setGruppe($value) {
        $this->gruppe = $value;
    }
    
    function Header() {
        global $basedir;
        global $VEREIN;
        $this->Image($basedir."/images/logo_kiga_jpg.jpg", 150, 10, 50);
        $this->SetFont('ShannonAtt','','10');
        $this->SetY(5);
        $vorstaende = Mitarbeiter::getVorstand(true);
        foreach($vorstaende as $value) {
            $adress = $value->getAdress();
            if($adress->isNameShow()) {
                $this->Cell(40,4,($adress->getVorname()." ".$adress->getNachname()));
            } 
            if($adress->isTelefonShow()) {
                $this->Cell(20,4,$adress->getTelefon());
            } 
            $this->Ln(4);
        }
        $this->Ln(4);
        $erstkraft = Mitarbeiter::getErstkraft(true, $this->gruppe->getId());
        foreach($erstkraft as $value) {
            $adress = $value->getAdress();
            if($adress->isNameShow()) {
                $this->Cell(40,4,($adress->getVorname()." ".$adress->getNachname()));
            } 
            if($adress->isTelefonShow()) {
                $this->Cell(40,4,$adress->getTelefon());
            } 
//            if($adress->isEMailErlaubt()) {
//                $this->Cell(50,4,$adress->getEMail());
//            }
            $this->Ln(4);
        }
        $zweitkraft = Mitarbeiter::getZweitkraft(true, $this->gruppe->getId());
        foreach ($zweitkraft as $value) {
            $adress = $value->getAdress();
            if($adress->isNameShow()) {
                $this->Cell(40,4,($adress->getVorname()." ".$adress->getNachname()));
            } 
            if($adress->isTelefonShow()) {
                $this->Cell(40,4,$adress->getTelefon());
            } 
//            if($adress->isEMailErlaubt()) {
//                $this->Cell(50,4,$adress->getEMail());
//            // }
            $this->Ln(4);
        }
        $this->SetY(5);
        $this->Ln(0);
        $this->Cell(80);
        $this->Cell(50,4,"michael-ol@waldorfkindergarten.de");
        $this->SetY(20);
        $this->SetFontSize(12);
        $this->Cell(190,5,($this->gruppe->getName()),0,0,"C");
        $this->SetY(10);
        $this->Ln(30);
    }
    
    function Footer() {
        global $basedir;
        $this->SetY(-14);
        $this->Image($basedir."/images/logo_pari_sw.png", 10, 284, 0, 8);
        $this->Image($basedir."/images/logo_waldorfvereinigung_sw.png", 162, 284, 0, 8);
        $this->SetFont("ShannonAtt",'',8);
        $this->Cell(200,3,"Michael-Kindergarten in Oldenburg e.V. | Bremer Str. 15 | 26135 Oldenburg",0,0,"C");
        $this->Ln(3);
        $this->Cell(200,3,"Landessparkasse zu Oldenburg | BLZ 28050100 | Konto-Nr: 23418809",0,0,"C");
        $this->Ln(3);
        $this->Cell(200,3,"IBAN: DE89280501000023418809 | BIC: BRLADE21LZO",0,0,"C");
        $this->Ln(3);
    }
    
    function ImageEps ($file, $x, $y, $w=0, $h=0, $link='', $useBoundingBox=true){
    
        $data = file_get_contents($file);
        if ($data===false) $this->Error('EPS file not found: '.$file);
    
        $regs = array();
    
        # EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
        preg_match ('/%%Creator:([^\r\n]+)/', $data, $regs); # find Creator
        if (count($regs)>1){
            $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
            if (strpos($version_str, 'Adobe Illustrator')!==false) {
                $version = (float)array_pop( explode(' ', $version_str) );
                if ($version>=9)
                    $this->Error('File was saved with wrong Illustrator version: '.$file);
                    #return false; # wrong version, only 1.x, 3.x or 8.x are supported
            }#else
              #$this->Error('EPS wasn\'t created with Illustrator: '.$file);
        }
    
        # strip binary bytes in front of PS-header
        $start = strpos($data, '%!PS-Adobe');
        if ($start>0) $data = substr($data, $start);
    
        # find BoundingBox params
        preg_match ("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
        if (count($regs)>1){
            list($x1,$y1,$x2,$y2) = explode(' ', trim($regs[1]));
        }
        else $this->Error('No BoundingBox found in EPS file: '.$file);
    
        $start = strpos($data, '%%EndSetup');
        if ($start===false) $start = strpos($data, '%%EndProlog');
        if ($start===false) $start = strpos($data, '%%BoundingBox');
    
        $data = substr($data, $start);
    
        $end = strpos($data, '%%PageTrailer');
        if ($end===false) $end = strpos($data, 'showpage');
        if ($end) $data = substr($data, 0, $end);
    
        # save the current graphic state
        $this->_out('q');
    
        $k = $this->k;
    
        if ($useBoundingBox){
            $dx = $x*$k-$x1;
            $dy = $y*$k-$y1;
        }else{
            $dx = $x*$k;
            $dy = $y*$k;
        }
    
        # translate
        $this->_out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', 1,0,0,1,$dx,$dy+($this->hPt - 2*$y*$k - ($y2-$y1))));
    
        if ($w>0){
            $scale_x = $w/(($x2-$x1)/$k);
            if ($h>0){
                $scale_y = $h/(($y2-$y1)/$k);
            }else{
                $scale_y = $scale_x;
                $h = ($y2-$y1)/$k * $scale_y;
            }
        }else{
            if ($h>0){
                $scale_y = $h/(($y2-$y1)/$k);
                $scale_x = $scale_y;
                $w = ($x2-$x1)/$k * $scale_x;
            }else{
                $w = ($x2-$x1)/$k;
                $h = ($y2-$y1)/$k;
            }
        }
    
        # scale
        if (isset($scale_x))
            $this->_out(sprintf('%.3f %.3f %.3f %.3f %.3f %.3f cm', $scale_x,0,0,$scale_y, $x1*(1-$scale_x), $y2*(1-$scale_y)));
    
        # handle pc/unix/mac line endings
        $lines = split ("\r\n|[\r\n]", $data);
    
        $u=0;
        $cnt = count($lines);
        for ($i=0;$i<$cnt;$i++){
            $line = $lines[$i];
            if ($line=='' || $line{0}=='%') continue;
    
            $len = strlen($line);
    
            $chunks = explode(' ', $line);
            $cmd = array_pop($chunks);
    
            # RGB
            if ($cmd=='Xa'||$cmd=='XA'){
                $b = array_pop($chunks); $g = array_pop($chunks); $r = array_pop($chunks);
                $this->_out("$r $g $b ". ($cmd=='Xa'?'rg':'RG') ); #substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
                continue;
            }
    
            switch ($cmd){
                case 'm':
                case 'l':
                case 'v':
                case 'y':
                case 'c':
    
                case 'k':
                case 'K':
                case 'g':
                case 'G':
    
                case 's':
                case 'S':
    
                case 'J':
                case 'j':
                case 'w':
                case 'M':
                case 'd' :
    
                case 'n' :
                case 'v' :
                    $this->_out($line);
                    break;
    
                case 'x': # custom fill color
                    list($c,$m,$y,$k) = $chunks;
                    $this->_out("$c $m $y $k k");
                    break;
    
                case 'X': # custom stroke color
                    list($c,$m,$y,$k) = $chunks;
                    $this->_out("$c $m $y $k K");
                    break;
    
                case 'Y':
                case 'N':
                case 'V':
                case 'L':
                case 'C':
                    $line{$len-1}=strtolower($cmd);
                    $this->_out($line);
                    break;
    
                case 'b':
                case 'B':
                    $this->_out($cmd . '*');
                    break;
    
                case 'f':
                case 'F':
                    if ($u>0){
                        $isU = false;
                        $max = min($i+5,$cnt);
                        for ($j=$i+1;$j<$max;$j++)
                            $isU = ($isU || ($lines[$j]=='U' || $lines[$j]=='*U'));
                        if ($isU) $this->_out("f*");
                    }else
                        $this->_out("f*");
                    break;
    
                case '*u':
                    $u++;
                    break;
    
                case '*U':
                    $u--;
                    break;
    
                #default: echo "$cmd<br>"; #just for debugging
            }
    
        }
    
        # restore previous graphic state
        $this->_out('Q');
        if ($link)
            $this->Link($x,$y,$w,$h,$link);
    
        return true;
    }

    function Content($gruppen_id,$mode) {
        $kinderlist = GruppenKinder::getAll(true, $gruppen_id, "2013-08-01");
        for($i=0; $i<sizeof($kinderlist); $i++) {
            $bottom = "";
            $kind = $kinderlist[$i]->getKind();
            $kind_nachname = $kind->isNameShow()||$mode=='all'?$kind->getNachname():"";
            $kind_geburtstag = $kind->isNameShow()||$mode=='all'?date("d.m.Y",strtotime($kind->getGeburtsdatum())):"";
            $kind_adresse = "";
            if($kind->isAdresseShow()||$mode=='all') {
                $adresse = $kind->getAdresse();
                $kind_adresse = $adresse->getStrasse();
                $kind_adresse .= $adresse->getHausnummer()!=""?" ".$adresse->getHausnummer():"";
                $kind_adresse .= $adresse->getZusatzHausnummer()!=""?" ".$adresse->getZusatzHausnummer():"";
                $kind_adresse .= ",".($adresse->getPlz()!=""?" ".$adresse->getPlz():"");
                $kind_adresse .= $adresse->getOrt()!=""?" ".$adresse->getOrt():"";
            }
            $kind_telefon = $kind->getAdresse()->isTelefonShow()||$mode=='all'?$kind->getAdresse()->getTelefon():"";
            $vater = $kind->getVater();
            $mutter = $kind->getMutter();
            $mutter_name = $mutter->isNameShow()||$mode=='all'?$mutter->getVorname()." ".$mutter->getNachname():"";
            $mutter_mobile = $mutter->isMobilShow()||$mode=='all'?$mutter->getMobil():"";
            $mutter_email = $mutter->isEMailShow()||$mode=='all'?$mutter->getEMail():"";
            $vater_name = $vater->isNameShow()||$mode=='all'?$vater->getVorname()." ".$vater->getNachname():"";
            $vater_mobile = $vater->isMobilShow()||$mode=='all'?$vater->getMobil():"";
            $vater_email = $vater->isEMailShow()||$mode=='all'?$vater->getEMail():"";
            $isMutter = false;
            if($mutter_email!="" || $mutter_mobile!="" || $mutter_name!="") {
                $isMutter = true;
            }
            $isVater = false;
            if($vater_email!="" || $vater_mobile!="" || $vater_name!="") {
                $isVater = true;
            }
            if($i+1==sizeof($kinderlist) && !$isMutter && !$isVater) {
                $bottom = "B";
            }
            $this->SetFontSize(10);
            $this->Cell(51,5,($kind->getVorname()." ".$kind_nachname),"LT".$bottom,0,"L");
            $this->Cell(20,5,$kind_geburtstag,"T".$bottom,0,"L");
            $this->Cell(80,5,($kind_adresse),"T".$bottom,0,"L");
            $this->Cell(30,5,$kind_telefon,"TR".$bottom,0,"L");
            $this->Ln(5);
            $this->SetFontSize(8);
            if($mutter_email!="" || $mutter_mobile!="" || $mutter_name!="") {
                if($i+1==sizeof($kinderlist) && !$isVater) {
                    $bottom = "B";
                }
                $this->Cell(51,3.5,($mutter_name),"L".$bottom,0,"L");
                $this->Cell(100,3.5,$mutter_email,"".$bottom,0,"L");
                $this->Cell(30,3.5,$mutter_mobile,"R".$bottom,0,"L");
                $this->Ln(3.5);
            }
            if($vater_email!="" || $vater_mobile!="" || $vater_name!="") {
                if($i+1==sizeof($kinderlist)) {
                    $bottom = "B";
                }
                $this->Cell(51,3.5,($vater_name),"L".$bottom,0,"L");
                $this->Cell(100,3.5,$vater_email,"".$bottom,0,"L");
                $this->Cell(30,3.5,$vater_mobile,"R".$bottom,0,"L");
                $this->Ln(3.5);
            }
        }
    }
}
